package com.ytjj.qmyx.admin.controller;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.eunm.GroupEnums;
import com.ytjj.qmyx.admin.model.BaseGroupInfo;
import com.ytjj.qmyx.admin.model.request.GroupActivityReq;
import com.ytjj.qmyx.admin.model.request.goods.GoodsRequest;
import com.ytjj.qmyx.admin.service.GroupActivityService;
import com.ytjj.qmyx.admin.utils.EmptyUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 *
 * 
 * 拼团活动页
 */
@RestController
@AllArgsConstructor
@RequestMapping("/groupActivity")
@Slf4j
public class GroupActivityController {

    private GroupActivityService groupActivityService;

    @PostMapping("/selectGroupList")
    @OperLog(operModule = "拼团活动页", operDesc = "拼团列表", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult selectGroupList(@RequestBody GroupActivityReq groupActivityReq) {
        return CommonResult.success(groupActivityService.selectGroupList(groupActivityReq));
    }

    @PostMapping(value = "/submit")
    @OperLog(operModule = "拼团活动页", operDesc = "新增商品", operType = SysLogConstants.OPERATION_ADD)
    public CommonResult insertGroup(@Valid @RequestBody BaseGroupInfo baseGroupInfo){
        if(EmptyUtil.isEmpty(baseGroupInfo)){
            return CommonResult.failed(GroupEnums.SPPT_IS_NULL.getMessage());
        }
        if(groupActivityService.judgeProduct(baseGroupInfo.getProductId())!=0
                || groupActivityService.judgeProduct(baseGroupInfo.getProductId()).equals("0")){
            return CommonResult.failed(GroupEnums.SP_EXIST.getMessage());
        }
        if(groupActivityService.judgeState(baseGroupInfo.getProductId()).length()>=2){
            return CommonResult.failed(GroupEnums.SP_YCZHD.getMessage());
        }
        if(groupActivityService.judgeH5(baseGroupInfo.getProductId()).equals("h5") ||
                groupActivityService.judgeH5(baseGroupInfo.getProductId())=="h5"){
            return CommonResult.failed(GroupEnums.PD_H5.getMessage());
        }
      return CommonResult.success(groupActivityService.insertGroup(baseGroupInfo)
              && groupActivityService.updateState(baseGroupInfo.getProductId()));
    }

    @GetMapping(value = "/delete")
    @OperLog(operModule = "拼团活动页", operDesc = "移除拼团", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult deleteGroup(Integer id,Integer productId){
        if (EmptyUtil.isEmpty(id)){
            return CommonResult.failed(GroupEnums.PT_IS_NULL.getMessage());
        }else if(EmptyUtil.isEmpty(productId)){
            return CommonResult.failed(GroupEnums.SP_IS_NULL.getMessage());
        }
        return CommonResult.success(groupActivityService.deleteGroup(id)
                && groupActivityService.updateActivity(productId));
    }

    @GetMapping(value = "/selectProduct")
    @OperLog(operModule = "拼团活动页", operDesc = "按商品ID查询商品信息", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult selectProduct(Integer productId){
        if (EmptyUtil.isEmpty(productId)){
            return CommonResult.failed(GroupEnums.SP_IS_NULL.getMessage());
        }
        return CommonResult.success(groupActivityService.selectProduct(productId));
    }

    @PostMapping(value = "/updateGroup")
    @OperLog(operModule = "拼团活动页", operDesc = "修改商品活动", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateGroup(@RequestBody BaseGroupInfo baseGroupInfo){
        return CommonResult.success(groupActivityService.updateGroup(baseGroupInfo));
    }

    @GetMapping("/detailGroup")
    @OperLog(operModule = "拼团详情", operDesc = "拼团详情", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult detailGroup(Integer id){
        if (EmptyUtil.isEmpty(id)) {
            return CommonResult.failed(GroupEnums.PT_IS_NULL.getMessage());
        }
        return CommonResult.success(groupActivityService.detailById(id));
    }

    @PostMapping(value = "/updateProduct")
    @OperLog(operModule = "拼团活动页", operDesc = "修改商品", operType = SysLogConstants.OPERATION_UPDATE)
    public CommonResult updateProduct(@RequestBody GoodsRequest goodsRequest){
        return CommonResult.success(groupActivityService.updateGroupPrice(goodsRequest.getGroupId(),goodsRequest.getGroupPrice())
                && groupActivityService.updateProduct(goodsRequest));
    }

}
